package com.wlstock.funi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.wlstock.funi.R;
import com.wlstock.funi.constant.SharePrefConstants;
import com.wlstock.funi.utils.SharePreferenceUtils;
import com.wlstock.funi.utils.ToastUtils;
import com.wlstock.funi.utils.TradeSetUtils;

public class SettingActivity extends FundBaseActivity implements OnClickListener{
	
	private Button btn_submit_set;
	private EditText ed_stop_loss;
	private EditText ed_stop_win;
	private static boolean isTrackSpecificON;
	
	private Button btn_track_specific;
	private EditText ed_voice_show_dis;
	private EditText ed_position;
	private CheckBox cb_operator_realtime;
	private CheckBox cb_operator_track;
	private EditText ed_default_money;
	private CheckBox cb_auto_buy;

	private CheckBox cb_debug_mode;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_setting);
		setTitle("设置");
		btn_submit_set = (Button) findViewById(R.id.btn_submit_set);
		btn_submit_set.setOnClickListener(this);
		
		ed_stop_loss = (EditText) findViewById(R.id.ed_stop_loss);
		ed_stop_win = (EditText) findViewById(R.id.ed_stop_win);
		ed_stop_loss.setText(TradeSetUtils.getLossOffset(this, TradeSetUtils.LOSE_OFFSET)+"");
		ed_stop_win.setText(TradeSetUtils.getLossOffset(this,  TradeSetUtils.WIN_OFFSET)+"");
		
		ed_voice_show_dis = (EditText) findViewById(R.id.ed_voice_show_dis);
		ed_voice_show_dis.setText(TradeSetUtils.getVoiceDis(this)+"");

		ed_position = (EditText) findViewById(R.id.ed_position);
		ed_position.setText(TradeSetUtils.getPosition(this)+"");
		
		btn_track_specific = (Button) findViewById(R.id.btn_track_specific);
		btn_track_specific.setOnClickListener(this);
		
		findViewById(R.id.btn_detect_queue).setOnClickListener(this);
		findViewById(R.id.btn_ai_trade_queue).setOnClickListener(this);


//		rdbtn_track_all.setChecked(trackMode==TradeSetUtils.TRACK_ALL);
//		rdbtn_track_front.setChecked(trackMode==TradeSetUtils.TRACK_FRONT);
		
		RadioGroup rdgroup_voice_set = (RadioGroup) findViewById(R.id.rdgroup_voice_set);
		RadioButton rdbtn_voice_off = (RadioButton) findViewById(R.id.rdbtn_voice_off);
		RadioButton rdbtn_voice_on = (RadioButton) findViewById(R.id.rdbtn_voice_on);
		isTrackSpecificON = SharePreferenceUtils.getBoolSP(this, TradeSetUtils.IS_VOICE_ON, false);
		rdbtn_voice_on.setChecked(isTrackSpecificON);
		rdbtn_voice_off.setChecked(!isTrackSpecificON);
		//作为一个优先检查的对象，如果设置了详细设置，则每次交易先从详细设置中获取，如果详细中没有针对该选中进行设置，则获取全局的设置;
		rdgroup_voice_set.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup arg0, int checkedId) {
				switch (checkedId) {
				case R.id.rdbtn_voice_off:
					SharePreferenceUtils.setBoolSP(SettingActivity.this, TradeSetUtils.IS_VOICE_ON, false);
					break;
				case R.id.rdbtn_voice_on:
					SharePreferenceUtils.setBoolSP(SettingActivity.this, TradeSetUtils.IS_VOICE_ON, true);
					break;
				default:
					break;
				}
			}
		});
		
		
		cb_operator_realtime = (CheckBox) findViewById(R.id.cb_operator_realtime);
		cb_operator_realtime.setChecked(SharePreferenceUtils.getBoolSP(this, SharePrefConstants.NOTI_OPERATE_SHOW, false));
		cb_operator_realtime.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
				SharePreferenceUtils.setBoolSP(SettingActivity.this, SharePrefConstants.NOTI_OPERATE_SHOW, isChecked);
			}
		});

		cb_operator_track = (CheckBox) findViewById(R.id.cb_operator_track);
		cb_operator_track.setChecked(SharePreferenceUtils.getBoolSP(this, SharePrefConstants.IS_WANGLONG_REQ, true));
		cb_operator_track.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				SharePreferenceUtils.setBoolSP(SettingActivity.this, SharePrefConstants.IS_WANGLONG_REQ, isChecked);
			}
		});

		ed_default_money = (EditText) findViewById(R.id.ed_default_money);
		ed_default_money.setText(SharePreferenceUtils.getFloatSP(this, SharePrefConstants.DEFAULT_MONEY, 6000)+"");
		ed_default_money.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				try{
					SharePreferenceUtils.setFloatSP(SettingActivity.this, SharePrefConstants.DEFAULT_MONEY,Float.valueOf(s.toString()));
				}catch (Exception e){
					SharePreferenceUtils.setFloatSP(SettingActivity.this, SharePrefConstants.DEFAULT_MONEY,5000);
					e.printStackTrace();
				}
			}
		});

		cb_auto_buy = (CheckBox) findViewById(R.id.cb_auto_buy);
		cb_auto_buy.setChecked(SharePreferenceUtils.getBoolSP(this,SharePrefConstants.IS_AUTO_BUY_MODE,false));
		cb_auto_buy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				SharePreferenceUtils.setBoolSP(SettingActivity.this,SharePrefConstants.IS_AUTO_BUY_MODE, isChecked);
			}
		});


		cb_debug_mode = (CheckBox)findViewById(R.id.cb_debug_mode);
		cb_debug_mode.setChecked(SharePreferenceUtils.getBoolSP(this,SharePrefConstants.IS_DEBUG_MODE,false));
		cb_debug_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				SharePreferenceUtils.setBoolSP(SettingActivity.this, SharePrefConstants.IS_DEBUG_MODE, isChecked);
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_submit_set:
			Float lose_offset = Float.valueOf(ed_stop_loss.getText().toString());
			Float win_offset = Float.valueOf(ed_stop_win.getText().toString());
			if (lose_offset<-0.15){
				ToastUtils.showMiddleMsg(this,"止损值为小数,设置的止损值过小,请重新设置");
				return;
			}
			if (win_offset>0.5){
				ToastUtils.showMiddleMsg(this,"止盈值为小数,设置的止盈值过大,请重新设置");
				return;
			}
			TradeSetUtils.setLossOffSet(this, TradeSetUtils.LOSE_OFFSET, lose_offset);
			TradeSetUtils.setLossOffSet(this, TradeSetUtils.WIN_OFFSET, win_offset);
			TradeSetUtils.setVoiceDis(this, Integer.valueOf(ed_voice_show_dis.getText().toString()));
			TradeSetUtils.setPosition(this, Float.valueOf(ed_position.getText().toString()));
			this.finish();
			break;
		case R.id.btn_track_specific:
			Intent i = new Intent(SettingActivity.this, SpecificSetActivity.class);
			startActivity(i);
			break;
		case R.id.btn_detect_queue:
			Intent detectIntent = new Intent(SettingActivity.this, SelfFundSetHisActivity.class);
			startActivity(detectIntent);
			break;
		case R.id.btn_ai_trade_queue:
			Intent hisIntent = new Intent(SettingActivity.this, TradeHisActivity.class);
			startActivity(hisIntent);
			break;
		default:
			break;
		}
	}
	
}	
